package com.sleepcamel.resteasyConfigurator.util;

import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.sleepcamel.rest.domain.JavaClass;

public class ClassProvider {

	private static ClassProvider instance;
	private static Map<String, JavaClass> classesMap;

	private ClassProvider(){
		classesMap = new HashMap<String, JavaClass>();
		registerClass(void.class);
		registerClass(int.class);
		registerClass(double.class);
		registerClass(String.class);
		registerClass(long.class);
		registerClass(Void.class);
		registerClass(Double.class);
		registerClass(Integer.class);
		registerClass(List.class);
		registerClass(Set.class);
		registerClass(Map.class);
		registerClass(Collection.class);
		registerClass(int[].class);
		registerClass(double[].class);
		registerClass(long[].class);
	}
	
	public void registerClass(Class<?> clazz) {
		JavaClass javaClass = new JavaClass(clazz.getSimpleName());
		javaClass.setPrimitive(clazz.isPrimitive());
		if ( clazz.getPackage() != null ){
			javaClass.setPackageName(clazz.getPackage().getName());
		}
		registerJavaClass(javaClass);
	}
	
	public void registerJavaClass(JavaClass clazz) {
		classesMap.put(clazz.getName(), clazz);
	}

	public static ClassProvider getInstance(){
		if ( instance == null ){
			instance = new ClassProvider();
		}
		return instance;
	}

	public JavaClass getClassFor(String className) {
		return classesMap.get(className);
	}
	
	public String[] getClassesNames() {
		return classesMap.keySet().toArray(new String[]{});
	}
}
